﻿﻿
using System;
using Newtonsoft.Json;  //引用命名空间


public class MsgBase : BdUtils.IJson
{
    public string protoName = "";

    public static byte[] Encode(MsgBase msgBase)
    {
        string s = msgBase.ToString();
        return System.Text.Encoding.UTF8.GetBytes(s);
    }

    public static MsgBase Decode(
        string protoName,
        byte[] bytes,
        int offset = -1,
        int count = -1
    )
    {
        string s = System.Text.Encoding.UTF8.GetString(bytes, offset, count);

        offset = offset == -1 ? 0 : offset;
        count = count == -1 ? s.Length : count;

        // Type personType = typeof(Person);
        Type protoType = Type.GetType(protoName);
        // Type protoType = Type.GetType("MyNamespace.Person, MyAssembly");
        MsgBase msgBase = (MsgBase)JsonConvert.DeserializeObject(s, protoType);
        return msgBase;
    }

    public static MsgBase Decode(
        string protoName,
        string bytes,
        int offset = -1,
        int count = -1
    )
    {
        string s = bytes;

        offset = offset == -1 ? 0 : offset;
        count = count == -1 ? s.Length : count;

        Type protoType = Type.GetType(protoName);
        // BdUtils.log("protoType, not_null, conv_to_bool --- ", protoType, !BdUtils.is_null(protoType), BdUtils.conv_to_bool(!BdUtils.is_null(protoType)));

        BdUtils.assert(!BdUtils.is_null(protoType), $"Error: protoType为null! protoName[{protoName}]未找到对应的Msg类型!");

        MsgBase msgBase = (MsgBase)JsonConvert.DeserializeObject(s, protoType);
        return msgBase;
    }

}


// MsgMove, 移动消息
public class MsgMove : MsgBase
{
    // public MsgMove() { protoName = "MsgMove"; }
    public MsgMove(int x, int y, int z)
    {
        protoName = "MsgMove";
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public int x = 0;
    public int y = 0;
    public int z = 0;

}


public class MsgAttack : MsgBase
{
    public MsgAttack(string target = "", string desc = "")
    {
        protoName = "MsgAttack";
        this.target = target;
        this.desc = desc;
    }

    public string target = "";

    public string desc = "";
}


public class MsgState : MsgBase
{
    public MsgState(object state, string info = "", int i = 0)
    {
        protoName = "MsgState";
        this.state = state.ToString();
        this.info = info;
        this.i = i;
    }

    public string state = "";

    public int i = 0;
    public string info = "";
}


public class MsgAction : MsgBase
{
    public MsgAction(int a, int i = 0)
    {
        protoName = "MsgAction";
        this.a = a;
        this.i = i;
    }

    public int a = 0;
    public int i = 0;
}


public class MsgReset : MsgBase
{
    public MsgReset(object state, string info = "")
    {
        protoName = "MsgReset";
        this.state = state.ToString();
        this.info = info;
    }

    public string state = "";

    public string info = "";
}

public class MsgTest : MsgBase
{
    public MsgTest(string info = "")
    {
        this.info = info;
        protoName = "MsgTest";
    }

    public string info = "";
}

public class MsgInterval : MsgBase
{
    public MsgInterval() { protoName = "MsgInterval"; }
    public int interval = 0;        // 操作间隔(毫秒)

}


public class MsgPing : MsgBase
{
    public MsgPing() { protoName = "MsgPing"; }
}


public class MsgPong : MsgBase
{
    public MsgPong() { protoName = "MsgPong"; }
}


public class MsgClose : MsgBase
{
    public MsgClose() { protoName = "MsgClose"; }
}